package fr.umlv.m2.jee.spij.service.registration;

import fr.umlv.m2.jee.spij.persistence.schooling.Registration;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Display an Registration. DTO pattern.
 * @author lounes
 *
 */
@Component("registrationBean")
public class DisplayRegistration implements Serializable {
    /**
     * the primary key.
     */
    private long id;

    /**
     * Instructional start time.
     */
    private Date startTime = new Date();

    /**
     * Instructional end time.
     */
    private Date endTime = new Date();

    /**
     *
     */
    private DateTime date;
    /**
     * the training name
     */
    private String training;
    /**
     * the course name
     */
    private String course;
    /**
     * the student login
     */
    private String student;
    /**
     * the validation boolean
     */
    private boolean validate;
    /**
     * the number of option
     */
    private String nbOption;
    /**
     * the list ofnitem course
     */
    private ArrayList<SelectItem> courseUnits = new ArrayList<SelectItem>();
    /**
     * the of units
     */
    private List<String> units = new ArrayList<String>();
    /**
     * the list of obligatory units
     */
    private List<String> requiredUnits = new ArrayList<String>();
    /**
     * Constructs a DTO registration from registration and training name.
     * @param registration
     * @param training
     */
    public DisplayRegistration(Registration registration, String training) {
        storeData(registration, training);
    }
    /**
     * Empty constructor
     */
    public DisplayRegistration() {
        this.id = -1L;
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addCourseUnits(SelectItem arg0) {
        return courseUnits.add(arg0);
    }

    /**
     * @param e
     *
     * @return
     *
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addRequiredUnit(String e) {
        return requiredUnits.add(e);
    }

    /**
     * @param index
     *
     * @return
     *
     * @see java.util.List#get(int)
     */
    public String getRequiredUnit(int index) {
        return requiredUnits.get(index);
    }

    /**
     * @param index
     *
     * @return
     *
     * @see java.util.List#remove(int)
     */
    public String removeRequiredUnit(int index) {
        return requiredUnits.remove(index);
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#add(java.lang.Object)
     */
    public boolean addUnit(String arg0) {
        return units.add(arg0);
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#get(int)
     */
    public String getUnit(int arg0) {
        return units.get(arg0);
    }

    /**
     * @param arg0
     *
     * @return
     *
     * @see java.util.List#remove(int)
     */
    public String removeUnit(int arg0) {
        return units.remove(arg0);
    }

    /**
     * @return
     *
     * @see java.util.List#size()
     */
    public int nbOfUnits() {
        return units.size();
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }
    /**
     * 
     * @return the start time
     */
    public Date getStartTime() {
        return new Date(startTime.getTime());
    }
    /**
     * 
     * @param startTime the start time to set
     */
    public void setStartTime(Date startTime) {
        this.startTime = new Date(startTime.getTime());
    }
    /**
     * 
     * @return the end time
     */
    public Date getEndTime() {
        return new Date(endTime.getTime());
    }
    /**
     * 
     * @param endTime the end time to set
     */
    public void setEndTime(Date endTime) {
        this.endTime = new Date(endTime.getTime());
    }

    /**
     * @return the date
     */
    public DateTime getDate() {
        return new DateTime(date);
    }

    /**
     * @param date the date to set
     */
    public void setDate(DateTime date) {
        this.date = new DateTime(date);
    }

    /**
     * @return the training
     */
    public String getTraining() {
        return training;
    }

    /**
     * @param training the training to set
     */
    public void setTraining(String training) {
        this.training = training;
    }

    /**
     * @param student the student to set
     */
    public void setStudent(String student) {
        this.student = student;
    }

    /**
     * @return the student
     */
    public String getStudent() {
        return student;
    }

    /**
     * @return the courseUnits
     */
    public ArrayList<SelectItem> getCourseUnits() {
        return courseUnits;
    }

    /**
     * @param courseUnits the courseUnits to set
     */
    public void setCourseUnits(ArrayList<SelectItem> courseUnits) {
        this.courseUnits = courseUnits;
    }

    /**
     * @return the units
     */
    public List<String> getUnits() {
        return units;
    }

    /**
     * @param units the units to set
     */
    public void setUnits(List<String> units) {
        this.units = units;
    }

    /**
     * Stores datas from registration into DisplayRegistration. 
     * @param registration the registration that is display
     * @param training 
     */
    private void storeData(Registration registration, String training) {
        if (registration.getId() != 0L) {
            setId(registration.getId());
        }
        if (registration.getDate() != null) {
            setDate(registration.getDate());
            setValidate(true);
        } else {
            setValidate(false);
        }
        if (registration.getYear() != null) {
            setStartTime(registration.getYear().getStart().toDate());
            setEndTime(registration.getYear().getEnd().toDate());
        }
        if (registration.getLogin() != null) {
            setStudent(registration.getLogin());
        }
        if (training != null) {
            setTraining(training);
        }
    }

    /**
     * @param validate the validate to set
     */
    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    /**
     * @return the validate
     */
    public boolean isValidate() {
        return validate;
    }

    /**
     * @param nbOption the nbOption to set
     */
    public void setNbOption(String nbOption) {
        this.nbOption = nbOption;
    }

    /**
     * @return the nbOption
     */
    public String getNbOption() {
        return nbOption;
    }

    /**
     * @param requiredUnits the requiredUnits to set
     */
    public void setRequiredUnits(List<String> requiredUnits) {
        this.requiredUnits = requiredUnits;
    }

    /**
     * @return the requiredUnits
     */
    public List<String> getRequiredUnits() {
        return requiredUnits;
    }

    /**
     * @param course the course to set
     */
    public void setCourse(String course) {
        this.course = course;
    }

    /**
     * @return the course
     */
    public String getCourse() {
        return course;
    }
}
